package com.k8.common.aop.internal.interceptor;

import com.k8.common.aop.internal.advice.AdviceOrderEnum;
import com.k8.common.aop.internal.advice.AfterThrowingAdvice;
import com.k8.common.aop.internal.invocation.MethodInvocation;
import com.k8.common.util.AopUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class AfterThrowingMethodInterceptor extends ProxyMethodExecutor implements AfterThrowingAdvice {
    @Override
    public int getAdviceOrder() {
        return AfterThrowingAdvice.super.getAdviceOrder();
    }

    public AfterThrowingMethodInterceptor(Object target, Method targetMethod) {
        super(target, targetMethod);
    }

    @Override
    boolean judgeArgument(Parameter parameter, Method targetMethod) {
        return AopUtil.judgeJoinAdviceArgs(parameter, AdviceOrderEnum.AfterThrowing,targetMethod);
    }

    @Override
    Object invoke0(MethodInvocation invocation) throws InvocationTargetException, IllegalAccessException {
        Object result =null;
        try {
            result=invocation.proceed();
        } catch (Throwable e) {
            super.invokeAdvice(e);
        }
        return result;
    }
}
